package com.ruoyi.common.utils.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.ruoyi.common.config.AliyunOssConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;

@Component
public class AliyunOssUploadUtils {

    private static AliyunOssConfig aliyunOssConfig;

    /**
     * 使用构造方法注入配置信息
     */
    @Autowired
    public AliyunOssUploadUtils(AliyunOssConfig aliyunOssConfig) {
        AliyunOssUploadUtils.aliyunOssConfig = aliyunOssConfig;
    }

    /**
     * 上传文件
     * @param file
     * @return
     * @throws Exception
     */
    public static String uploadFile(MultipartFile file) throws Exception {
        // 生成 OSSClient
        OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());
        // 原始文件名称
        // String originalFilename = file.getOriginalFilename();
        // 编码文件名
        String filePathName = FileUploadUtils.extractFilename(file);
        // 文件路径名称
        filePathName = aliyunOssConfig.getFilehost() + "/" + filePathName;
        try {
            ossClient.putObject(aliyunOssConfig.getBucketName(), filePathName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return  "/" + filePathName;
       // return aliyunOssConfig.getUrl() + "/" + filePathName;
    }


    public static String uploadFileBase64(String base64Image) throws Exception {
        // 生成 OSSClient
        OSS ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());

        String filePathName = null;
        try {
            // 解码Base64字符串
            base64Image  = base64Image.contains(",") ? base64Image.split(",")[1] : base64Image;
            byte[] imageBytes = Base64.getDecoder().decode(base64Image);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);

            filePathName = aliyunOssConfig.getFilehost() + "/" + "wx"+System.currentTimeMillis() + ".png";
            // 上传文件到OSS
            ossClient.putObject(aliyunOssConfig.getBucketName(), filePathName, inputStream);
            return  "/" + filePathName;
            //return aliyunOssConfig.getUrl() + "/" + filePathName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }

    }


    public static void main(String[] args) {
        String base64Image = ""; // 确保这个字符串是有效的Base64
        try {
            base64Image  = base64Image.contains(",") ? base64Image.split(",")[1] : base64Image;
            byte[] decodedBytes = Base64.getDecoder().decode(base64Image);
            // 处理解码后的数据
        } catch (IllegalArgumentException e) {
            System.err.println("非法Base64字符: " + e.getMessage());
            // 进一步的错误处理
        }
    }

}
